﻿namespace QLPhongKham.BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDToaThuocNgoaiVienCT
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private long _deid;
        private int _deiddp;
        private long _deidtoathuoc;
        private decimal _desoluong;
        private decimal _desoluongchieu;
        private decimal _desoluongsang;
        private decimal _desoluongtoi;
        private decimal _desoluongtrua;
        private int _iiddangnhap;
        private int _isongay;
        private string _scachdung;
        private string _sngaycn;
        private string _sSchema;
        private string _stenmaytinh;

        public dtoDToaThuocNgoaiVienCT()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDToaThuocNgoaiVienCT(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDToaThuocNgoaiVienCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1 and ", columnID, " = ", this._deid }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDToaThuocNgoaiVienCT.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDToaThuocNgoaiVienCT.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDToaThuocNgoaiVienCT.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDToaThuocNgoaiVienCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnIDTOATHUOC, ",", columnIDDP, ",", columnSOLUONGSANG, ",", columnSOLUONGTRUA, ",", columnSOLUONGCHIEU, 
                    ",", columnSOLUONGTOI, ",", columnSOLUONG, ",", columnSONGAY, ",", columnCACHDUNG, ",", columnIDDANGNHAP, ",", columnTENMAYTINH, ") values(", this._deid, ",", this._deidtoathuoc, 
                    ",", this._deiddp, ",:", columnSOLUONGSANG, ",:", columnSOLUONGTRUA, ",:", columnSOLUONGCHIEU, ",:", columnSOLUONGTOI, ",:", columnSOLUONG, ",:", columnSONGAY, ",:", columnCACHDUNG, 
                    ",", this._iiddangnhap, ",:", columnTENMAYTINH, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnCACHDUNG, NpgsqlDbType.Text, 200).Value = this._scachdung;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnSOLUONGSANG, NpgsqlDbType.Numeric, 20).Value = this._desoluongsang;
                command.Parameters.Add(columnSOLUONGTRUA, NpgsqlDbType.Numeric, 20).Value = this._desoluongtrua;
                command.Parameters.Add(columnSOLUONGCHIEU, NpgsqlDbType.Numeric, 20).Value = this._desoluongchieu;
                command.Parameters.Add(columnSOLUONGTOI, NpgsqlDbType.Numeric, 20).Value = this._desoluongtoi;
                command.Parameters.Add(columnSOLUONG, NpgsqlDbType.Numeric, 20).Value = this._desoluong;
                command.Parameters.Add(columnSONGAY, NpgsqlDbType.Numeric, 20).Value = this._isongay;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDToaThuocNgoaiVienCT.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0L;
            this._deidtoathuoc = 0L;
            this._deiddp = 0;
            this._desoluongsang = 0M;
            this._desoluongtrua = 0M;
            this._desoluongchieu = 0M;
            this._desoluongtoi = 0M;
            this._desoluong = 0M;
            this._isongay = 0;
            this._scachdung = "";
            this._iiddangnhap = 0;
            this._stenmaytinh = "";
            this._sngaycn = "";
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDToaThuocNgoaiVienCT.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnID, ",", columnIDTOATHUOC, ",", columnIDDP, ",", columnSOLUONGSANG, ",", columnSOLUONGTRUA, ",", columnSOLUONGCHIEU, ",", columnSOLUONGTOI, ",", columnSOLUONG, 
                    ",", columnSONGAY, ",", columnCACHDUNG, ",", columnIDDANGNHAP, ",", columnTENMAYTINH, ",to_char(", columnNGAYCN, ",'dd/mm/yyyy hh24:mi') as ", columnNGAYCN, " from ", this._sSchema, ".", tableName, 
                    " where ", columnID, "=", this._deid
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deid = long.Parse(dataTable.Rows[0][columnID].ToString());
                    this._deidtoathuoc = long.Parse(dataTable.Rows[0][columnIDTOATHUOC].ToString());
                    this._deiddp = int.Parse(dataTable.Rows[0][columnIDDP].ToString());
                    this._desoluongsang = decimal.Parse(dataTable.Rows[0][columnSOLUONGSANG].ToString());
                    this._desoluongtrua = decimal.Parse(dataTable.Rows[0][columnSOLUONGTRUA].ToString());
                    this._desoluongchieu = decimal.Parse(dataTable.Rows[0][columnSOLUONGCHIEU].ToString());
                    this._desoluongtoi = decimal.Parse(dataTable.Rows[0][columnSOLUONGTOI].ToString());
                    this._desoluong = decimal.Parse(dataTable.Rows[0][columnSOLUONG].ToString());
                    this._isongay = int.Parse(dataTable.Rows[0][columnSONGAY].ToString());
                    this._scachdung = dataTable.Rows[0][columnCACHDUNG].ToString();
                    this._iiddangnhap = int.Parse(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                    this._stenmaytinh = dataTable.Rows[0][columnTENMAYTINH].ToString();
                    this._sngaycn = dataTable.Rows[0][columnNGAYCN].ToString();
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDToaThuocNgoaiVienCT.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDToaThuocNgoaiVienCT.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnIDTOATHUOC + "," + columnIDDP + "," + columnSOLUONGSANG + "," + columnSOLUONGTRUA + "," + columnSOLUONGCHIEU + "," + columnSOLUONGTOI + "," + columnSOLUONG + "," + columnSONGAY + "," + columnCACHDUNG + "," + columnIDDANGNHAP + "," + columnTENMAYTINH + ",to_char(" + columnNGAYCN + ",'dd/mm/yyyy hh24:mi:ss') as " + columnNGAYCN + " from " + this._sSchema + "." + tableName + " order by " + columnID, connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDToaThuocNgoaiVienCT.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDToaThuocNgoaiVienCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnIDTOATHUOC, " = ", this._deidtoathuoc, ",", columnIDDP, " = ", this._deiddp, ",", columnSOLUONGSANG, " = :", columnSOLUONGSANG, 
                    ",", columnSOLUONGTRUA, " = :", columnSOLUONGTRUA, ",", columnSOLUONGCHIEU, " = :", columnSOLUONGCHIEU, ",", columnSOLUONGTOI, " = :", columnSOLUONGTOI, ",", columnSOLUONG, " = :", columnSOLUONG, 
                    ",", columnSONGAY, " = :", columnSONGAY, ",", columnCACHDUNG, " = :", columnCACHDUNG, ",", columnIDDANGNHAP, " = ", this._iiddangnhap, ",", columnTENMAYTINH, " = :", columnTENMAYTINH, 
                    ",", columnNGAYCN, " = now()  where ", columnID, " = ", this._deid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnCACHDUNG, NpgsqlDbType.Text, 200).Value = this._scachdung;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnSOLUONGSANG, NpgsqlDbType.Numeric, 20).Value = this._desoluongsang;
                command.Parameters.Add(columnSOLUONGTRUA, NpgsqlDbType.Numeric, 20).Value = this._desoluongtrua;
                command.Parameters.Add(columnSOLUONGCHIEU, NpgsqlDbType.Numeric, 20).Value = this._desoluongchieu;
                command.Parameters.Add(columnSOLUONGTOI, NpgsqlDbType.Numeric, 20).Value = this._desoluongtoi;
                command.Parameters.Add(columnSOLUONG, NpgsqlDbType.Numeric, 20).Value = this._desoluong;
                command.Parameters.Add(columnSONGAY, NpgsqlDbType.Numeric, 20).Value = this._isongay;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDToaThuocNgoaiVienCT.cs");
                return false;
            }
        }

        public static string columnCACHDUNG
        {
            get
            {
                return "cachdung";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnIDDP
        {
            get
            {
                return "iddp";
            }
        }

        public static string columnIDTOATHUOC
        {
            get
            {
                return "idtoathuoc";
            }
        }

        public static string columnNGAYCN
        {
            get
            {
                return "ngaycn";
            }
        }

        public static string columnSOLUONG
        {
            get
            {
                return "soluong";
            }
        }

        public static string columnSOLUONGCHIEU
        {
            get
            {
                return "soluongchieu";
            }
        }

        public static string columnSOLUONGSANG
        {
            get
            {
                return "soluongsang";
            }
        }

        public static string columnSOLUONGTOI
        {
            get
            {
                return "soluongtoi";
            }
        }

        public static string columnSOLUONGTRUA
        {
            get
            {
                return "soluongtrua";
            }
        }

        public static string columnSONGAY
        {
            get
            {
                return "songay";
            }
        }

        public static string columnTENMAYTINH
        {
            get
            {
                return "tenmaytinh";
            }
        }

        private string NGAYCN
        {
            get
            {
                return this._sngaycn;
            }
            set
            {
                this._sngaycn = value;
            }
        }

        public string pCACHDUNG
        {
            get
            {
                return this._scachdung;
            }
            set
            {
                this._scachdung = value;
            }
        }

        public long pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._iiddangnhap;
            }
            set
            {
                this._iiddangnhap = value;
            }
        }

        public int pIDDP
        {
            get
            {
                return this._deiddp;
            }
            set
            {
                this._deiddp = value;
            }
        }

        public long pIDTOATHUOC
        {
            get
            {
                return this._deidtoathuoc;
            }
            set
            {
                this._deidtoathuoc = value;
            }
        }

        public decimal pSOLUONG
        {
            get
            {
                return this._desoluong;
            }
            set
            {
                this._desoluong = value;
            }
        }

        public decimal pSOLUONGCHIEU
        {
            get
            {
                return this._desoluongchieu;
            }
            set
            {
                this._desoluongchieu = value;
            }
        }

        public decimal pSOLUONGSANG
        {
            get
            {
                return this._desoluongsang;
            }
            set
            {
                this._desoluongsang = value;
            }
        }

        public decimal pSOLUONGTOI
        {
            get
            {
                return this._desoluongtoi;
            }
            set
            {
                this._desoluongtoi = value;
            }
        }

        public decimal pSOLUONGTRUA
        {
            get
            {
                return this._desoluongtrua;
            }
            set
            {
                this._desoluongtrua = value;
            }
        }

        public int pSONGAY
        {
            get
            {
                return this._isongay;
            }
            set
            {
                this._isongay = value;
            }
        }

        public string pTENMAYTINH
        {
            get
            {
                return this._stenmaytinh;
            }
            set
            {
                this._stenmaytinh = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_toathuoc_ngoaivienct";
            }
        }
    }
}

